#!/bin/bash
REDIS_PATH=/usr/local
REDIS_PORT=6379
REDIS_PWD=123456
#VERSION=redis-7.0.2

#输出颜色设置
color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

#调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile

#Require root to run this script.
[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本！" && exit 1

if [ $# != 1 ]; then
	echo "Usage: redis-install.sh redis*.gz"
	exit 1
fi

#下载安装包
#wget http://download.redis.io/releases/${VERSION}.tar.gz 

echo "编译安装"
tar -zxvf $1 -C  ${REDIS_PATH} &>/dev/null && mv ${REDIS_PATH}/redis-* ${REDIS_PATH}/redis
cd ${REDIS_PATH}/redis &>/dev/null && make &>/dev/null && make install  &>/dev/null
sleep 3
# check install result
${REDIS_PATH}/redis/src/redis-server -v
if [ $? -eq 0 ] ;then
    echo "*****redis install successfully!*****"
else
    echo "*****redis install error!*****"
    exit $?
fi

echo "redis配置"
  sed -i "s/port 6379/port ${REDIS_PORT}/g"   ${REDIS_PATH}/redis/redis.conf   #sed替换变量需要把单引号改成双引号
  sed -i 's/daemonize no/daemonize yes/g'   ${REDIS_PATH}/redis/redis.conf
  sed -i 's/protected-mode yes/protected-mode no/g'   ${REDIS_PATH}/redis/redis.conf
  sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g'   ${REDIS_PATH}/redis/redis.conf
cat >>  ${REDIS_PATH}/redis/redis.conf << EOF
requirepass ${REDIS_PWD}
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command EVAL ""
EOF

cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p 

echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local

cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${REDIS_PATH}/redis/src/redis-server ${REDIS_PATH}/redis/redis.conf 
ExecStop=${REDIS_PATH}/redis/src/redis-cli -h 127.0.0.1 -p ${REDIS_PORT} -a ${REDIS_PWD} shutdown
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload 
systemctl enable --now  redis &> /dev/null && color "Redis 服务启动成功,Redis信息如下:"  0 || { color "Redis 启动失败" 1 ;exit; } 
sleep 2
${REDIS_PATH}/redis/src/redis-cli -a $PASSWORD INFO Server 2> /dev/null


echo -e "\033[33m***************************************************完成redis环境部署***************************************************\033[0m"
cat << EOF |tee /tmp/redis-install.log 
redis安装目录：${REDIS_PATH}/redis
redis端口：${REDIS_PORT}
redis密码：${REDIS_PWD}
redis启动/停止：systemctl start/stop redis
EOF